package com.guotd.jspwork2.controller;

import com.guotd.jspwork2.utils.JDBCUtil;
import org.hsqldb.Session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import static com.guotd.jspwork2.utils.JDBCUtil.getConnection;

@WebServlet("/getAllTVs")
public class getAllTVsServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        List<Object[]> tvList = new ArrayList<>();
        String kindName = request.getParameter("kind");
        /*try {Connection con = getConnection();
             PreparedStatement pstmt = con.prepareStatement("SELECT * FROM tv where kind = ?");
             pstmt.setString(1, kindName);
             ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Object[] row = new Object[6];
                row[0] = rs.getString("id");
                row[1] = rs.getString("name");
                row[2] = rs.getDouble("price");
                row[3] = rs.getString("picture");
                row[4] = rs.getInt("hz");
                row[5] = rs.getString("kind");
                tvList.add(row);
            }
        } catch (SQLException e) {
            throw new ServletException(e);
        }*/
        String sql="SELECT * FROM tv where kind = ?";
        String []args={kindName};
        String []colNames ={"id","name","price","picture","hz","kind"};
        tvList=JDBCUtil.query(sql,args,colNames);
        request.getSession().setAttribute("tvList", tvList);
        request.getSession().setAttribute("kind", kindName);
        response.sendRedirect("tv_list.jsp");
    }
}